package com.netease.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netease.utils.DateUtil;
import com.netease.utils.PagedGridResult;
import org.n3r.idworker.Sid;
import com.netease.service.RootInfoService;
import com.netease.entity.Word;
import com.netease.mapper.RootInfoMapper;
import com.netease.pojo.RootInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @projectName: modeling-dev
 * @package: com.netease.impl
 * @className: RootInfoServiceImpl
 * @author: aochong
 * @description: TODO
 * @date: 2023/3/31 16:26
 * @version: 1.0
 */
@Service
public class RootInfoServiceImpl implements RootInfoService {
    @Autowired
    private RootInfoMapper rootInfoMapper;

    @Autowired
    private Sid sid;

    @Override
    public void createRoot(Word word) {
        RootInfo rootInfo = new RootInfo();
        //设置唯一主键ID
        rootInfo.setId(sid.nextShort());
        //设置词根所属业务板块
        rootInfo.setBsinssPlate("国贸集团");
        //设置词根中文名称
        rootInfo.setRootChnsNm(word.getName());
        //设置词根英文翻译
        rootInfo.setTranslt(word.getEngName());
        //设置词根英文缩写
        rootInfo.setRootNm(word.getEngAbbreviation());
        //设置词频
        rootInfo.setWordFrequency(word.getWordFrequency());
        //设置是否评审
        rootInfo.setIsReview("是");
        //设置插入时间
        rootInfo.setInsertTime(DateUtil.dateToString(new Date()));
        //设置更新时间(第一次和插入时间一样)
        rootInfo.setUpdateTime(DateUtil.dateToString(new Date()));

        rootInfoMapper.insert(rootInfo);
    }

    @Override
    public void updateRoot(int id) {
//        rootInfoMapper.updateByPrimaryKey(root)
    }

    @Override
    public void updateRoot(RootInfo rootInfo) {
        rootInfoMapper.updateByPrimaryKey(rootInfo);
    }

    @Override
    public void batchUpdate(List<RootInfo> rootInfos) {
        for (RootInfo rootInfo : rootInfos) {
            Example example = new Example(RootInfo.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("rootChnsNm",rootInfo.getRootChnsNm());
            RootInfo rootInfoOld = rootInfoMapper.selectOneByExample(example);
            //词根存在则更新
            if (rootInfoOld != null){
                rootInfo.setId(rootInfoOld.getId());
                rootInfo.setBsinssPlate(rootInfoOld.getBsinssPlate());
                rootInfo.setInsertTime(rootInfoOld.getInsertTime());
                rootInfo.setUpdateTime(DateUtil.dateToString(new Date()));
                rootInfoMapper.updateByPrimaryKey(rootInfo);
            }else{
                rootInfo.setId(sid.nextShort());
                rootInfo.setInsertTime(DateUtil.dateToString(new Date()));
                rootInfo.setUpdateTime(DateUtil.dateToString(new Date()));
                rootInfo.setBsinssPlate("国贸集团");
                //不存在则插入
                rootInfoMapper.insert(rootInfo);
            }
        }
    }

    @Override
    public void deleteRoot(int id) {
        rootInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public RootInfo queryRoot(Word word) {
        Example example = new Example(RootInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("rootChnsNm",word.getName());
        return rootInfoMapper.selectOneByExample(example);
    }

    @Override
    public boolean queryRootIsExist(Word word) {
        Example example = new Example(RootInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("rootChnsNm",word.getName());
        RootInfo rootInfo = rootInfoMapper.selectOneByExample(example);

        return rootInfo != null;
    }

    @Override
    public PagedGridResult queryPagedRoots(Integer page,Integer pageSize) {
        /**
         * page: 第几页
         * pageSize: 每页显示条数
         */
        PageHelper.startPage(page, pageSize);
        List<RootInfo> rootInfos = rootInfoMapper.selectAll();
        PagedGridResult pagedGridResult = setterPagedGrid(rootInfos, page);

        return pagedGridResult;
    }

    public static PagedGridResult setterPagedGrid(List<?> list, Integer page) {
        PageInfo<?> pageList = new PageInfo<>(list);
        PagedGridResult grid = new PagedGridResult();
        grid.setPage(page);
        grid.setRows(list);
        grid.setTotal(pageList.getPages());
        grid.setRecords(pageList.getTotal());
        return grid;
    }
}
